package com.yang.designmode.factory.FactoryMethod;

/**
 * 工厂方法模式
 */
public class FactoryMethod {
    public static void main(String[] args) {
        CarFactory  audiFactory = new AudiFactory();
        Car car = audiFactory.createCar();
        car.run();
        CarFactory  bydFactory = new BYDFactory();
        Car car1 = bydFactory.createCar();
        car1.run();
    }
}

interface Car {
    void run();
}

//定义Car的实现类
class Audi implements Car {
    @Override
    public void run() {
        System.out.println("Audi is running...");
    }
}

class BYD implements Car {
    @Override
    public void run() {
        System.out.println("BYD is running...");
    }
}

interface CarFactory {
    Car createCar();
}

class AudiFactory implements CarFactory {
    @Override
    public Car createCar() {
        return new Audi();
    }
}

class BYDFactory implements CarFactory {
    @Override
    public Car createCar() {
        return new BYD();
    }
}